package com.sunshine.lnuplusweb.controller;

import com.sunshine.lnuplusweb.entities.Lesson;
import com.sunshine.lnuplusweb.service.LessonService;
import com.sunshine.lnuplusweb.service.UserConfigService;
import com.sunshine.lnuplusweb.utils.BackgroundUtils;
import com.sunshine.lnuplusweb.utils.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 主界面控制器
 */
@Controller
public class IndexController {

    @Resource
    private UserConfigService userConfigService;

    @Resource
    private LessonService lessonService;

    @GetMapping("/logout")
    public String logout(Map<String, Object> map, HttpSession httpSession) {
        if (httpSession.getAttribute("userId") != null) {
            //移除用户登录信息
            httpSession.removeAttribute("userId");
        }
        //返回首页
        return index(map, httpSession);
    }

    @GetMapping({"/","/index", "/index.html"})
    public String index(Map<String, Object> map, HttpSession httpSession) {
        //预加载背景图片，等到其他需要的界面直接用
        BackgroundUtils.getBackgroundUrl(new Date());

        //WeatherVO weatherVO;

        if (httpSession.getAttribute("userId") != null) {
            map.put("login", true);
            map.put("userId", "欢迎 " + httpSession.getAttribute("userId"));

            //获取天气
            //weatherVO = WeatherUtils.getWeather((String) httpSession.getAttribute("userId"));

            int scheduleId = userConfigService.getNowScheduleId((String) httpSession.getAttribute("userId"));
            List<Lesson> lessonList = lessonService.getAllLessonByScheduleId(scheduleId);

            //添加今日课程信息
            if (null != lessonList) {
                int lessonCount = 0;
                List<Lesson> todayLessonList = new ArrayList<>();
                String thisWeek = String.valueOf(DateUtils.getThisWeek());
                int thisDay = DateUtils.getDayOfWeek(Calendar.getInstance().get(Calendar.DAY_OF_WEEK));
                for (Lesson lesson : lessonList) {
                    List<String> weeks = Arrays.asList(lesson.getWeeks().split(","));
                    if (weeks.contains(thisWeek)) {
                        if (thisDay == lesson.getDayOfWeek()) {
                            lessonCount++;
                            todayLessonList.add(lesson);
                        }
                    }
                }
                todayLessonList.sort(new LessonComparator());
                map.put("lesson_info", "今天您有" + lessonCount + "节课");
                map.put("today_lesson", todayLessonList);
            } else {
                map.put("lesson_info", "您还没有导入课程");
            }
        } else {
            //不登录获取天气，一般来说就是不准确的
            //weatherVO = WeatherUtils.getWeather("not_login");
            map.put("login", false);

            //添加今日课程信息
            map.put("lesson_info", "您还没有登录无法查看课表");
        }

        //背景图片地址
        map.put("background_url", BackgroundUtils.getBackgroundUrl(new Date()));

        //天气
        //map.put("weather", weatherVO);

        map.putAll(initTodayInfo());

        return "index";
    }

    /**
     * 传顶部今天信息，包括时间，天气，几节课
     */
    private Map<String, Object> initTodayInfo() {
        Map<String, Object> map = new HashMap<>();

        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 EE");
        map.put("date", format.format(Calendar.getInstance().getTime()));

        map.put("background_info", BackgroundUtils.getBackgroundInfo(new Date()));


        return map;
    }

    /**
     * 用于今日课程列表的按上课时间排序
     */
    class LessonComparator implements Comparator<Lesson> {

        @Override
        public int compare(Lesson o1, Lesson o2) {
            return o1.getStart() - o2.getStart();
        }
    }
}
